package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"os"
	"time"
	"web_runner/ai_model/sadTalker/jobmgr"
	"web_runner/ginutil"
)

var port = "9000"
var dst = "./"

func init() {
	dir, err := os.Getwd()
	if err == nil && dir != "" {
		dst = dir
		log.Println("已设置存放目录为当前工作目录：", dir)
	}
}
func main() {
	jobmgr.ScheduleJob("*/5 * * * * ?", func() {
		fmt.Println("hello world", time.Now())
		select {} //阻塞主线程停止
	})
	//新建已封装的gin示例
	r := ginutil.NewBasicRouter()
	r.GET("/", func(c *gin.Context) {
		urls := []string{"upload", "2", "3"}
		resultJson := gin.H{
			"name":   "admin",
			"server": "localhost",
			"port":   port,
			"urls":   urls,
		}
		ginutil.Response(c, resultJson)
	})
	//r.POST("/upload/one", uploadFile)
	r.POST("/upload/run", uploadAndRun)
	r.GET("/upload", uploadPage)
	r.GET("/test/result", downloadResult)
	r.GET("/test/1", func(c *gin.Context) {

		c.String(200, res)
	})
	r.Run(":" + port)
}

func downloadResult(c *gin.Context) {
	dir := "H:\\python_demo\\tts_generated"
	c.File(dir + "/before2.mp4")
}

func uploadAndRun(c *gin.Context) {
	//ginutil.DataMapByRequest(c) //debug
	audioFile, _ := c.FormFile("audio")
	imageFile, _ := c.FormFile("image")

	log.Println("已收到音频文件：", audioFile.Filename)
	log.Println("已收到视频文件：", imageFile.Filename)
	fmt.Println("dst", dst)
	//c.SaveUploadedFile(file, dst)
	c.JSON(200, gin.H{
		"status": "0",
		"msg":    "done.",
	})
}

func uploadPage(c *gin.Context) {
	c.HTML(200, "upload.tmpl", gin.H{
		"port": port,
	})
}

//func uploadFile(c *gin.Context) {
//	c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, int64(2<<20))
//	//form表单
//	file, header, err := c.Request.FormFile("upload")
//	fmt.Println("收到文件。")
//	if err != nil {
//		c.String(http.StatusBadRequest, fmt.Sprintf("上传文件失败: %s", err.Error()))
//		return
//	}
//	// 获取文件名，并创建新的文件存储
//	filename := header.Filename
//	out, err := os.Create(filename)
//	if err != nil {
//		c.String(http.StatusBadRequest, fmt.Sprintf("创建文件: %s", err.Error()))
//		return
//	}
//
//	defer out.Close()
//	//将读取的文件流写到文件中
//	_, err = io.Copy(out, file)
//	if err != nil {
//		c.String(http.StatusBadRequest, fmt.Sprintf("读取文件失败: %s", err.Error()))
//		return
//	}
//
//	c.String(http.StatusCreated, "上传成功 \n")
//}
